package edu.caece.langprocessor.syntax.parse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.caece.langprocessor.lexical.tokens.*;
import edu.caece.langprocessor.lexical.LexicalProcessor;
import edu.caece.langprocessor.syntax.ProductionFactory;
import edu.caece.langprocessor.syntax.Terminal;
import edu.caece.langprocessor.syntax.tree.Tree;

public class NoAction extends Action {

	private static final Log LOGGER = LogFactory.getLog(NoAction.class);;

	public NoAction(){
		this.type = ActionType.noAction;
	}
	
	@Override
	public ActionResult run(LexicalProcessor lexicalProcessor,
			Map<Integer, ProductionFactory> productions,
			GotoActionsTable gotoActionsTable,
			Stack<ParseStackItem> parseStack, Terminal terminalItem, 
			Tree parseTree) {
	
		// si action == null o el token es error
		// estoy en una situacion de error
		// avanzo el token para ver si puedo recuperarme del error
		// sin apilarlo
		if (LOGGER.isWarnEnabled()) {
			StringBuilder str = new StringBuilder();
			if (terminalItem.getLexicalToken() instanceof ErrorLexicalToken) {
				str.append("Invalid token: ");
			} else {
				str.append("Unexpected token: ");
			}
			str.append("Token: "
					+ terminalItem.getLexicalToken().getToken()
					+ " Lexema: "
					+ terminalItem.getLexicalToken().getLexema()
					+ " Line: "
					+ terminalItem.getLexicalToken().getLine()
					+ " Column: "
					+ terminalItem.getLexicalToken().getColumn());

			LOGGER.warn(str.toString());
		}
		List<Terminal> errorList = new ArrayList<Terminal>();
		errorList.add(terminalItem);
		
		return new ActionResult(new Terminal(lexicalProcessor.getNextLexicalToken()), null, 1, errorList);
		
		
	}

}
